//
//  LQComposePictureView.swift
//  Weibo
//
//  Created by ios on 16/8/16.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
//设置cell标记
private let ComposePictureViewCellIdentifier = "ComposePictureViewCellIdentifier"
class LQComposePictureView: UICollectionView {
    
    //图片数据源
    lazy var images: [UIImage] = [UIImage]()
    
   //添加图片的闭包
    var didSeletedAddImg:(()->())?
    
       override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI(){
    //  默认状态当前配图隐藏
    hidden = true
    //数据源代理
    dataSource = self
    //代理方法
    delegate = self
    //注册cell
    registerClass(LQComposePictureViewCell.self, forCellWithReuseIdentifier: ComposePictureViewCellIdentifier)
    
    }
    
    
    
    //  添加图片方法
    func addImage(image: UIImage) {
        //最多显示九张图片
        if images.count > 9 {
          return
        }
        
        //显示配图
        hidden = false
        images.append(image)

        //  重写加载数据
        self.reloadData()
        
    }
    

    override func layoutSubviews() {
        super.layoutSubviews()
        
        //  设置每项的间距
        let itemMargin: CGFloat = 5
        
        let itemWidth = (width - 2 * itemMargin) / 3
        //  获取布局方式
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //  设置大小
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //  设置水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        //  设置垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
    }
    
}

extension LQComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if images.count == 0 || images.count == 9 {
         return images.count
        }else{
         return images.count + 1
        }
       
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ComposePictureViewCellIdentifier, forIndexPath: indexPath) as! LQComposePictureViewCell
        if indexPath.item == images.count {
            cell.image = nil
        }else{
           cell.image = images[indexPath.item]
            
            //设置删除图片闭包
            cell.closeButtonCluse = { [weak self] in
              //  通过数据源删除指定图片
                self?.images.removeAtIndex(indexPath.item)
                
             //判断当前数据源是否为0
                if self?.images.count == 0{
                 
                 self?.hidden =  true
                }
                self?.reloadData()
            }
        }
        
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
         //  如果索引等于我们数组的个数表示最后一个cell
        if indexPath.item == images.count {
        //取消选中
            collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        //执行添加闭包
            didSeletedAddImg?()
        }
        
    }


}

class LQComposePictureViewCell:UICollectionViewCell {
    //设置一个闭包 点击删除按钮执行
     var closeButtonCluse:(()->())?
    var image:UIImage?{
    didSet{
        //设置图片

        if image == nil {
            imageView.image = UIImage(named: "compose_pic_add")
            //  添加高亮图片
            imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
            //  如果是设置加号图片那么隐藏删除按钮
           closeButton.hidden = true
        }else{
            imageView.image = image
            //显示删除按钮
            closeButton.hidden = false
            //删除高亮图片
             imageView.highlightedImage = nil
        }
        
    
    }
       
    }
     //MARK: --懒加载控件
    private lazy var imageView:UIImageView = {
       let ImgView = UIImageView()
        return ImgView
    }()
    
    //删除按钮
    private lazy var closeButton:UIButton = {
        let button = UIButton()
        //添加删除点击时间
        button.addTarget(self, action: "closeButtonAction", forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件 设置约束
    private func setupUI(){
        
        addSubview(imageView)
        addSubview(closeButton)
        
        //设置约束
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        closeButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
        
    }
    
    //删除按钮的点击事件
   @objc private func closeButtonAction(){
   
    closeButtonCluse?()
    }
}






